/**
 *
 */
package com.ybkj.daijia.server.service.impl;

import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.common.Principal;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mapper.CompanyMapper;
import com.ybkj.daijia.server.mapper.DriverLevelDetailMapper;
import com.ybkj.daijia.server.mapper.DriverLevelMapper;
import com.ybkj.daijia.server.mapper.DriverLevelMoneyDetailMapper;
import com.ybkj.daijia.server.mapper.DriverLevelMoneyTimeDetailMapper;
import com.ybkj.daijia.server.model.DriverLevel;
import com.ybkj.daijia.server.model.DriverLevelDetail;
import com.ybkj.daijia.server.model.DriverLevelMoneyDetail;
import com.ybkj.daijia.server.model.DriverLevelMoneyTimeDetail;
import com.ybkj.daijia.server.service.DriverLevelService;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * @author Shine
 *
 */
@Service
public class DefaultDriverLevelService implements DriverLevelService {

    @Autowired
    private DriverLevelMapper driverLevelMapper;

    @Autowired
    private DriverLevelMoneyTimeDetailMapper driverLevelMoneyTimeDetailMapper;

    @Autowired
    private DriverLevelMoneyDetailMapper driverLevelMoneyDetailMapper;

    @Autowired
    private DriverLevelDetailMapper driverLevelDetailMapper;

    @Autowired
    private CompanyMapper companyMapper;


    public Page<DriverLevel> queryAll(final Long companyid, final String name) {

        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());

        Subject currentUser = SecurityUtils.getSubject();
        Principal principal = (Principal) currentUser.getPrincipal();

        Long companyId = null;
        String likeName = null;

        if (!principal.isCompanyType()) {
            companyId = companyid;
        }

        if (StringUtils.isNotEmpty(name)) {
            likeName = "%" + name + "%";
        }

        List<DriverLevel> content = driverLevelMapper
            .selectByCompanyAndNameLike(companyId, name, Pager.getOffset(), Pager.getLimit());
        long total = driverLevelMapper.countByCompanyAndNameLike(companyId, likeName);

        if (!CollectionUtils.isEmpty(content)) {
            for (DriverLevel driverLevel : content) {
                driverLevel.setCompanyName(
                    companyMapper.selectByPrimaryKey(driverLevel.getCompanyId()).getAbbreviation());
            }
        }

        return new PageImpl<DriverLevel>(content, pageable, total);
    }

    @Override
    public List<DriverLevel> listMyAll() {

        Subject currentUser = SecurityUtils.getSubject();
        Principal principal = (Principal) currentUser.getPrincipal();

        return driverLevelMapper.selectByCompany(principal.getCompanyId());

    }

    @Override
    public List<DriverLevel> listByCompany(Long companyId) {

        return driverLevelMapper.selectByCompany(companyId);
    }

    @Override
    public List<DriverLevel> listAll() {

        return driverLevelMapper.selectByCompany(null);
    }

    @Transactional(rollbackFor = Exception.class)
    public DriverLevel insertRecord(DriverLevel driverLevel) {

        driverLevel.setType(0);
        driverLevelMapper.insertRecord(driverLevel);

        if (!CollectionUtils.isEmpty(driverLevel.getDriverLevelDetails())) {

            for (DriverLevelDetail driverLevelDetail : driverLevel.getDriverLevelDetails()) {
                driverLevelDetail.setDriverLevelId(driverLevel.getId());
            }

            driverLevelDetailMapper.batchInsertRecord(driverLevel.getDriverLevelDetails());
        }

        return driverLevel;

    }

    @Transactional(rollbackFor = Exception.class)
    public DriverLevel insertMoneyRecord(DriverLevel driverLevel) {

        driverLevel.setType(1);
        driverLevelMapper.insertRecord(driverLevel);

        if (!CollectionUtils.isEmpty(driverLevel.getDriverLevelMoneyTimeDetails())) {
            for (DriverLevelMoneyTimeDetail dlmtd : driverLevel.getDriverLevelMoneyTimeDetails()) {
                if (!dlmtd.isEmpty()) {
                    dlmtd.setDriverLevelId(driverLevel.getId());
                    driverLevelMoneyTimeDetailMapper.insertRecord(dlmtd);

                    List<DriverLevelMoneyDetail> moneylist = new LinkedList<DriverLevelMoneyDetail>();
                    if (!CollectionUtils.isEmpty(dlmtd.getDriverLevelMoneyDetails())) {
                        for (DriverLevelMoneyDetail dlmd : dlmtd.getDriverLevelMoneyDetails()) {
                            if (!dlmd.isEmpty()) {
                                dlmd.setMoneyTimeId(dlmtd.getId());
                                moneylist.add(dlmd);
                            }
                        }
                    }

                    if (!CollectionUtils.isEmpty(moneylist)) {
                        driverLevelMoneyDetailMapper.batchInsertRecord(moneylist);
                    }
                }
            }
        }

        return driverLevel;
    }

    @Transactional(rollbackFor = Exception.class)
    public DriverLevel updateByPrimaryKey(DriverLevel driverLevel) {

        driverLevel.setType(0);
        driverLevelMapper.updateByPrimaryKey(driverLevel);

        //删除提成详细信息
        driverLevelDetailMapper.deleteByDriverLevel(driverLevel.getId());

        //保存提成详细信息
        if (!CollectionUtils.isEmpty(driverLevel.getDriverLevelDetails())) {

            for (DriverLevelDetail driverLevelDetail : driverLevel.getDriverLevelDetails()) {
                driverLevelDetail.setDriverLevelId(driverLevel.getId());
            }

            driverLevelDetailMapper.batchInsertRecord(driverLevel.getDriverLevelDetails());
        }

        return driverLevel;
    }

    @Transactional(rollbackFor = Exception.class)
    public DriverLevel updateMoneyRecord(DriverLevel driverLevel) {

        driverLevel.setType(1);
        driverLevelMapper.updateByPrimaryKey(driverLevel);

        //删除提成详细信息
        driverLevelMoneyDetailMapper.deleteByDriverLeavel(driverLevel.getId());
        driverLevelMoneyTimeDetailMapper.deleteByDriverLevel(driverLevel.getId());

        //保存提成详细信息
        if (!CollectionUtils.isEmpty(driverLevel.getDriverLevelMoneyTimeDetails())) {
            for (DriverLevelMoneyTimeDetail dlmtd : driverLevel.getDriverLevelMoneyTimeDetails()) {
                if (!dlmtd.isEmpty()) {
                    dlmtd.setDriverLevelId(driverLevel.getId());
                    driverLevelMoneyTimeDetailMapper.insertRecord(dlmtd);

                    List<DriverLevelMoneyDetail> moneylist = new LinkedList<DriverLevelMoneyDetail>();
                    if (!CollectionUtils.isEmpty(dlmtd.getDriverLevelMoneyDetails())) {
                        for (DriverLevelMoneyDetail dlmd : dlmtd.getDriverLevelMoneyDetails()) {
                            if (!dlmd.isEmpty()) {
                                dlmd.setMoneyTimeId(dlmtd.getId());
                                moneylist.add(dlmd);
                            }
                        }
                    }

                    if (!CollectionUtils.isEmpty(moneylist)) {
                        driverLevelMoneyDetailMapper.batchInsertRecord(moneylist);
                    }
                }
            }
        }

        return driverLevel;
    }

    public DriverLevel findOne(Long id) {

        return driverLevelMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(DriverLevel driverLevel) {

        //删除提成详情
        driverLevelDetailMapper.deleteByDriverLevel(driverLevel.getId());
        //TODO 删除司机关联

        //删除提成
        driverLevelMapper.deleteByPrimaryKey(driverLevel.getId());

    }


    @Transactional(rollbackFor = Exception.class)
    public void deleteByDriverLevel(Long driverLevelId) {

        driverLevelDetailMapper.deleteByDriverLevel(driverLevelId);

    }

}